<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/

// Check to ensure this file is within the rest of the framework
defined('JPATH_BASE') or die();

/**
 * Renders a layout manager
 *
 * @package 	JoomlaVi Project
 * @subpackage	Parameter
 * @since		1.5
 */
require_once (JPATH_COMPONENT.DS.'libraries'.DS.'framework.php');
require_once (JPATH_COMPONENT.DS.'libraries'.DS.'layout'.DS.'admin.layouts.php');

class JElementLayoutmanager extends JElement {

    var $_name = 'Layoutmanager';
    var $document;
    var $params;
    var $_activetheme;
    /**
     * Template Sidebar
     */
    var $sidebar1 = 'sidebar1';
    var $sidebar2 = 'sidebar2';

    /**
     * Element type
     *
     * @access	protected
     * @var		string
     */
    function fetchElement($name, $value, &$node, $control_name){
        $jdocument = JDocumentHTML::getInstance(); $jdocument->template = JVTEMPLATE;        
        $jvadmin   = JVFactory::getFramework($jdocument, 'admin');
        $jvadmin->initialise();        
        $params = JVFactory::getParameter();        
        $layout = JVAdminLayout::getInstance($params)->getLayout();        
        $html   = $this->getLayoutToolbar();
        $html  .= $jvadmin->render();
        
        return $html;
    }
    
    public function getLayoutToolbar(){
        $tmp_path  = JVPATH_ROOT .'themes'.DS. $this->getActiveTheme()->theme . DS .'customlayouts' .DS;
        if (!is_dir($tmp_path)) JFolder::create($tmp_path);
        $files     = JFolder::files($tmp_path,'xml');  
             
        $options[] = JHTML::_('select.option', '-1', '- ' . JText::_('Do not use') .' -');  
        if (is_array($files)) {
            foreach ($files as $file) {
                $options[] = JHTML::_('select.option', $file, $file);
            }
        }    
           
        $input  = '<div id="layout-tool">'.JHTML::_('select.genericlist', $options,'chooselayout', 'class="inputbox"', 'value', 'text', '', 'choose-layout');
        $input .= ' <button onclick="jvAdmin.loadLayout(); return false;" type="button">Load</button>';
        $input .= ' <button onclick="jvAdmin.deleteLayout(); return false;" type="button">Delete</button>';
        $input .= ' <button onclick="jvAdmin.saveLayout(); return false;" type="button">Save as...</button>';
        $input .= ' <button onclick="jvAdmin.resetLayout(); return false;" type="button">Reset</button>';
        $input .= '</div><br />';   
        
        return $input;       
    }
    
    function fetchTooltip($label, $description, &$node, $control_name, $name) {	   
	}

    /**
     * Check current direction
     * 
     * @access public 
     * @return bool true on rtl direction, false on ltr direction.
     */
    function isRTL(){
        $jdocument = JDocument::getInstance();
        return ($jdocument->getDirection() == 'rtl' || JVParam::getDirection() == 'rtl');
    }
    
    /**
     * Parameter 
     * 
     * @access public
     * @return void
     */
    function getActiveTheme() {
        if($this->_activetheme == null){
            $cids = JRequest::getVar ( 'cid', array (0 ), 'method', 'array');  
            
            if(preg_match('/[a-zA-Z]/',$cids[0])){
                $this->_activetheme = $cids[0];
            } else {
                $data =& JFactory::getDBO();        
                $data->setQuery('SELECT id, theme FROM #__jv_themes WHERE id='.$cids[0]); 
                $resulf = $data->loadObject();
                $resulf->theme = current(explode('|', $resulf->theme));
                
                $this->_activetheme = $resulf;   
            }
        }
        
        return $this->_activetheme;
    }
  }
  
  class JVAdmin extends JV {
    var $_activetheme;
    /**
     * Get Instance JVFrameWork
     * 
     * @param  object $document JDocumentHTML object.
     * @access public     
     * @return object JV
     */
    public static function &getInstance($document = null) {        
        static $instance = null;        
        if (!isset($instance)) {
            $instance = new JVAdmin($document);
        }
        return $instance;
    }
    
    /**
     * Initialise
     * 
     * @access public
     * @return void
     */
    public function initialise(){
        $params = JVFactory::getParameter(); 
        
        $this->html =  JVAdminLayout::getInstance($params)->getLayout();
    }
    
     /**
     * Render doccument
     * 
     * @access public
     * @return string $html
     */
    public function render(){ 
        return $this->html;
    }
    
     /**
     * Get Active Theme 
     * 
     * @access public
     * @return void
     */
    function getActiveTheme() {
        if($this->_activetheme == null){
            $cids = JRequest::getVar ( 'cid', array (0 ), 'method', 'array');  
            
            if(preg_match('/[a-zA-Z]/',$cids[0])){
                $this->_activetheme = $cids[0];
            } else {
                $data =& JFactory::getDBO();        
                $data->setQuery('SELECT id, theme FROM #__jv_themes WHERE id='.$cids[0]); 
                $resulf = $data->loadObject();
                $resulf->theme = current(explode('|', $resulf->theme));
                
                $this->_activetheme = $resulf;   
            }
        }
        
        return $this->_activetheme;
    }
  }